package com.wanmait.recruit.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.recruit.mapper.CollectionMapper;
import com.wanmait.recruit.mapper.PositionMapper;
import com.wanmait.recruit.mapper.PositionTypeMapper;
import com.wanmait.recruit.pojo.*;
import com.wanmait.recruit.pojo.Collection;
import com.wanmait.recruit.service.*;
import com.wanmait.recruit.util.PageContext;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.mysql.cj.xdevapi.JsonArray;
import jdk.nashorn.internal.ir.debug.JSONWriter;
import net.sf.json.JSONArray;
import java.util.ArrayList;
import java.util.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/web/job")
public class JobController {
    @Resource
    private IndustryService industryService;
    @Resource
    private PositionAttrService positionAttrService;
    @Resource
    private WorkTimeService workTimeService;
    @Resource
    private EducationalBackgroundTypeService educationalBackgroundTypeService;
    @Resource
    private CompanyPositionService companyPositionService;
    @Resource
    PositionTypeMapper positionTypeMapper;
    @Resource
    ResumeService resumeService;
    @Resource
    CompanyHrService companyHrService;
    @Resource
    CompanyResumeService companyResumeService;
    @Resource
    CollectionMapper collectionMapper;
    @Resource
    private PositionTypeService positionTypeService;
    @Resource
    private PositionService positionService;
    @Resource
    CompanyAuthcService companyAuthcService;
    @Resource
    PersonInfoService personInfoService;
    @Resource
    private CompanyAdminService companyAdminService;

    @RequestMapping("/myCollectData")
    public ModelAndView myCollectData(HttpSession session){
        PageHelper.startPage(PageContext.getPageNum(),1);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/web/job/myCollectData");
        Person person = (Person)session.getAttribute("person");
        List<CompanyPosition> companyPositions = collectionMapper.findByPersonId(person.getId());
        PageInfo<CompanyPosition> pageInfo = new PageInfo<>(companyPositions);
        mv.addObject(pageInfo);
        return mv;
    }

    @RequestMapping("/myCollect")
    public ModelAndView myCollect(HttpSession session){
        PageHelper.startPage(PageContext.getPageNum(),1);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/web/job/myCollect");
        Person person = (Person)session.getAttribute("person");
        if(person==null){
            mv.addObject("msg","notLogin");
            mv.setViewName("/web/index");
            return mv;
        }
        List<CompanyPosition> companyPositions = collectionMapper.findByPersonId(person.getId());
        PageInfo<CompanyPosition> pageInfo = new PageInfo<>(companyPositions);
        mv.addObject(pageInfo);
        return mv;
    }

    //取消收藏工作
    @RequestMapping("/uncollectJob")
    public void uncollectJob(Integer id,HttpSession session,HttpServletResponse response) throws IOException {
        Person person = (Person)session.getAttribute("person");
        Collection collection = new Collection();
        collection.setPersonId(person.getId());
        collection.setCompanyPositionId(id);
        collectionMapper.delete(collection);
        PrintWriter out = response.getWriter();
        out.print(1);
        out.close();
    }

    //收藏工作
    @RequestMapping("/collectJob")
    public void collectJob(Integer id,HttpSession session,HttpServletResponse response) throws IOException {
        Person person = (Person)session.getAttribute("person");
        Collection collection = new Collection();
        collection.setPersonId(person.getId());
        collection.setCompanyPositionId(id);
        collectionMapper.insertSelective(collection);
        PrintWriter out = response.getWriter();
        out.print(1);
        out.close();
    }


    @RequestMapping("/check")
    public void check(Integer id, HttpSession session,HttpServletResponse response) throws IOException {
        Person person = (Person)session.getAttribute("person");
        Resume resume = resumeService.selectOneByPerson(person);

        CompanyHrReceiveResume companyHrReceiveResume = null;
        try {
            companyHrReceiveResume = companyResumeService.findByResume(resume,id);
        } catch (Exception e) {
        }
        if(companyHrReceiveResume==null&&resume!=null){
            PrintWriter out = response.getWriter();
            out.print(0);
            out.close();
        }
        else {
            PrintWriter out = response.getWriter();
            out.print(1);
            out.close();
        }

    }
    @RequestMapping("/ask")
    public void ask(Integer id, HttpSession session,HttpServletResponse response) throws IOException {

        CompanyHrReceiveResume companyHrReceiveResume = new CompanyHrReceiveResume();
        //设置职位ID
        companyHrReceiveResume.setCompanyPositionId(id);
        Person person = (Person)session.getAttribute("person");
        //查找简历
        Resume resume = resumeService.selectOneByPerson(person);
        companyHrReceiveResume.setResumeId(resume.getId());
        Person hr = companyPositionService.findById(id).getPerson();
        CompanyHr companyHr = companyHrService.findByPerson(hr);
        //设置HRid
        companyHrReceiveResume.setCompanyHrId(companyHr.getId());
        //保存到数据库
        companyResumeService.insert(companyHrReceiveResume);
        //向该HR发送消息
        PersonInfo personInfo = new PersonInfo();
        personInfo.setContent("您刚刚收到了一份简历，请前往查看");
        personInfo.setPersonId(hr.getId());
        personInfo.setResourcePersonId(person.getId());
        personInfoService.insert(personInfo);

        PrintWriter out = response.getWriter();
        out.print(1);
    }

    @RequestMapping("/findPositionType")
    @ResponseBody
    public List<PositionType> findPositionType(Integer industryId, HttpServletResponse response) throws IOException {
        List<PositionType> positionTypes = positionTypeMapper.findByIndustryId(industryId);
        return positionTypes;
    }

    @RequestMapping("list")
    public ModelAndView list(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("industries",industryService.findAll());
        modelAndView.addObject("positionAttrs",positionAttrService.findAll());
        modelAndView.addObject("workTimes",workTimeService.findAll());
        modelAndView.addObject("educationalTypes",educationalBackgroundTypeService.findAll());
        modelAndView.setViewName("/web/job/list");
        return modelAndView;
    }
    @RequestMapping("search")
    public ModelAndView search(Integer pageNum,Integer pageSize,String cityName,CompanyPosition companyPosition,HttpServletRequest request){
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/web/job/search");
        City city = new City();
        city.setCityName(cityName);
        companyPosition.setCity(city);

        PositionType positionType = new PositionType();
        positionType.setId(companyPosition.getPositionTypeId());
        companyPosition.setPositionType(positionType);

        EducationalBackgroundType educationalBackgroundType = new EducationalBackgroundType();
        educationalBackgroundType.setId(companyPosition.getEducationalBackgroundTypeId());
        companyPosition.setEducationalBackgroundType(educationalBackgroundType);

        PositionAttr positionAttr = new PositionAttr();
        positionAttr.setId(companyPosition.getPositionAttrId());
        companyPosition.setPositionAttr(positionAttr);

        WorkTime workTime = new WorkTime();
        workTime.setId(companyPosition.getWorkTimeId());
        companyPosition.setWorkTime(workTime);

        PageInfo<CompanyPosition> pageInfo = companyPositionService.searchRes(pageNum,pageSize,companyPosition);
        mv.addObject(pageInfo);
        return mv;
    }
    @RequestMapping("/single")
    public ModelAndView single(Integer CPId,HttpSession session){
        ModelAndView mv = new ModelAndView();
        CompanyPosition companyPosition = companyPositionService.findById(CPId);
        mv.addObject(companyPosition);
        Company company = companyPosition.getCompany();
        CompanyAuthc companyAuthc = companyAuthcService.findByCompany(company);
        mv.addObject(companyAuthc);
        Person person = (Person)session.getAttribute("person");
        if(person!=null){
            Collection collection = new Collection();
            collection.setPersonId(person.getId());
            collection.setCompanyPositionId(companyPosition.getId());
            collection = collectionMapper.findByCollection(collection);
            mv.addObject("collection",collection);
        }
        mv.setViewName("/web/job/single");
        return mv;
    }
    @RequestMapping("findJob")
    public ModelAndView findJob(String selectPosition,String positionOrCompanyName){
        /*
         * 公司表  company
         * 岗位信息表   company_position
         * 职位表  position
         * 职位类型表   position_type
         *行业表  industry
         *
         * */
        ModelAndView modelAndView = new ModelAndView();
        //System.out.println(selectPosition);
        try {

            int x = 0;
            for (int i = 0; i < selectPosition.length(); i++) {
                char c = selectPosition.charAt(i);
                if (c == 47) {
                    x = i;
                }
            }
            //System.out.println(selectPosition.substring(x+1));
            modelAndView.addObject("selectedPosition", selectPosition.substring(x + 1));
        }
        catch (Exception e){
            modelAndView.addObject("selectedPosition", selectPosition);
        }

        modelAndView.addObject("positionOrCompanyName",positionOrCompanyName);

        List<Industry> industries = industryService.findAll();
        modelAndView.addObject("industries",industries);

        List<PositionType> positionTypes = positionTypeService.findAll();
        modelAndView.addObject("positionTypes",positionTypes);

        ArrayList<Position> positions = positionService.selectFindAll();
        modelAndView.addObject("positions",positions);

        modelAndView.setViewName("/web/job/findJob");
        return modelAndView;
    }
    @RequestMapping("findPositionTypeByIndustryId")
    @ResponseBody
    public void findPositionTypeByIndustryId(Integer industryId, HttpServletResponse response){

        //System.out.println(industryId);
        List<PositionType> positionTypesByIndustryId=positionTypeService.findAllByIndustryId(industryId);

        response.setContentType("text/json");
        response.setCharacterEncoding("utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        //集合要调用json对象数组JSONArray类
        JSONArray jsonObject = JSONArray.fromObject(positionTypesByIndustryId);
        //单个对象要调用jsonobject类
        //JSONObject jsonObject2 = JSONObject.fromObject(positionTypesByIndustryId);
        out.println(jsonObject.toString());
        //out.println(jsonObject2.toString());
    }
    @RequestMapping("listData")
    public ModelAndView listData(Integer industryId,Integer pageNum){
        //Integer typeId,Integer positionId,
        //typeId,positionId,
        ModelAndView modelAndView=new ModelAndView();
        PageInfo<CompanyPosition> pageInfo = companyPositionService.searchByIndustryId(industryId,pageNum);
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.setViewName("/web/job/jobData/jobData");

        return modelAndView;

    }
    @RequestMapping("listData1")
    public ModelAndView listData1(Integer typeId,Integer pageNum){
        //Integer typeId,Integer positionId,
        //typeId,positionId,
        ModelAndView modelAndView=new ModelAndView();
        PageInfo<CompanyPosition> pageInfo = companyPositionService.searchByTypeId(typeId,pageNum);
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.setViewName("/web/job/jobData/jobData1");
        return modelAndView;

    }
    @RequestMapping("listData2")
    public ModelAndView listData2(Integer positionId,Integer pageNum){
        ModelAndView modelAndView=new ModelAndView();
        PageInfo<CompanyPosition> pageInfo = companyPositionService.searchByPositionId(positionId,pageNum);
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.setViewName("/web/job/jobData/jobData2");
        return modelAndView;

    }
    @RequestMapping("listData3")
    public ModelAndView listData3(String title,String selectedPosition){
        ModelAndView modelAndView=new ModelAndView();
        PageInfo<CompanyPosition> pageInfo = companyPositionService.searchByCompanyOrPosition(title,selectedPosition);
        modelAndView.addObject("pageInfo",pageInfo);
        modelAndView.setViewName("/web/job/jobData/jobData3");
        return modelAndView;

    }

}
